﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using egypt.Logic;
using System.Collections.Generic;
using egypt.Logic.Data;

namespace egypt.Web
{
    public partial class lyfw : System.Web.UI.Page
    {
        DataAdapter da = null;
        int pageIndex = 1;
        int fuwuID = 0;
        int page = 0;
        List<string> fuwu = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.Request.QueryString["Page"] != null)
                {
                    int tmp_pageIndex = 0;
                    if (int.TryParse(Page.Request.QueryString["Page"], out tmp_pageIndex))
                        this.pageIndex = tmp_pageIndex;
                }
                else
                {
                    this.pageIndex = 1;
                }

                if (Page.Request.QueryString["fid"] != null)
                {
                    int.TryParse(Page.Request.QueryString["fid"], out fuwuID);
                }

                Bind();
            }
        }

        private void Bind()
        {

            da = new DataAdapter(System.Configuration.ConfigurationManager.ConnectionStrings["egypt"].ConnectionString);

            this.fuwu = da.GetFuwuName();

            int pagesize = 0;
            if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["PageSize"], out pagesize))
                pagesize = 10;

            List<News> tmpData = da.GetNewsList(6, pagesize, pageIndex,this.fuwuID,0, out page);
            this.GridView1.DataSource = tmpData;
            this.GridView1.DataBind();
            if (this.fuwuID > 0)
            {
                this.hy_first.NavigateUrl = string.Format("?fid={1}&page={0}", 1,this.fuwuID);
                this.hy_pre.NavigateUrl = string.Format("?fid={1}&page={0}", pageIndex > 1 ? (pageIndex - 1).ToString() : "1", this.fuwuID);
                this.hy_next.NavigateUrl = string.Format("?fid={1}&page={0}", pageIndex < page ? (pageIndex + 1).ToString() : page.ToString(), this.fuwuID);
                this.hy_last.NavigateUrl = string.Format("?fid={1}&page={0}", page, this.fuwuID);


                if (this.pageIndex > page)
                    this.pageIndex = page;

                for (int i = 1; i <= page; i++)
                {
                    HyperLink hl = new HyperLink();
                    hl.NavigateUrl = string.Format("?fid={1}&page={0}", i,this.fuwuID);
                    hl.Text = i.ToString();
                    hl.CssClass = "pager";
                    if (i == pageIndex)
                    {
                        hl.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
                    }
                    this.pager_link.Controls.Add(hl);
                }
            }
            else
            {
                this.hy_first.NavigateUrl = string.Format("?page={0}", 1);
                this.hy_pre.NavigateUrl = string.Format("?page={0}", pageIndex > 1 ? (pageIndex - 1).ToString() : "1");
                this.hy_next.NavigateUrl = string.Format("?page={0}", pageIndex < page ? (pageIndex + 1).ToString() : page.ToString());
                this.hy_last.NavigateUrl = string.Format("?page={0}", page);


                if (this.pageIndex > page)
                    this.pageIndex = page;

                for (int i = 1; i <= page; i++)
                {
                    HyperLink hl = new HyperLink();
                    hl.NavigateUrl = string.Format("?page={0}", i);
                    hl.Text = i.ToString();
                    hl.CssClass = "pager";
                    if (i == pageIndex)
                    {
                        hl.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
                    }
                    this.pager_link.Controls.Add(hl);
                }
            }
        }

        protected string GetFuwu(object fuwuID)
        {
            int id = 0;
            if (this.fuwu != null && int.TryParse(fuwuID.ToString(), out id) && id>0)
                return string.Format("[{0}]&nbsp;",this.fuwu[id-1]);

            return "";
        }
    }
    }
